/**
 * @JedisRpcServerNamingService.java  COPYRIGHT xiaojian
 * 
 * Created on 2017年1月6日 上午9:56:58
 */
package xj.toolkit.rpc.naming.jedis;

import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;

import xj.toolkit.redis.Jedis2Commands;
import xj.toolkit.rpc.RpcService;
import xj.toolkit.rpc.naming.RpcNode;
import xj.toolkit.rpc.naming.RpcServerNamingService;

/**
 * @author <a href="mailto:cxj2000@gmail.com">xiaojian.cao</a>
 *
 */
@Service("rpcServerNamingService")
public class JedisRpcServerNamingService extends JedisRpcNamingService implements RpcServerNamingService {

	private static final Logger logger = LoggerFactory.getLogger(JedisRpcServerNamingService.class);

	@Autowired
	private Jedis2Commands jedis = null;

	@Value("${node.name:localhost_rpc}")
	private String nodeName;

	/**
	 * @see xj.toolkit.rpc.naming.RpcServerNamingService#registerNode(xj.toolkit.rpc.naming.RpcNode)
	 */
	@Override
	public RpcNode registerNode(RpcNode rpcNode) {
		Preconditions.checkArgument(
				rpcNode != null && StringUtils.isNoneBlank(rpcNode.getIpAddress()) && rpcNode.getPort() > 0);

		String nodeKey = String.format(RpcRedisKeyConstants.RPC_NODE, nodeName);
		rpcNode.setName(nodeName);
		if (jedis.exists(nodeKey)) {
			logger.warn("Update node information[{}].", rpcNode);
		}

		jedis.hset(nodeKey, RpcRedisKeyConstants.RPC_NODE_ATTR_ADDR, rpcNode.getIpAddress());
		jedis.hset(nodeKey, RpcRedisKeyConstants.RPC_NODE_ATTR_PORT, String.valueOf(rpcNode.getPort()));

		return rpcNode;
	}

	@Override
	public RpcNode registerNode(String address, int port) {
		Preconditions.checkArgument(StringUtils.isNoneBlank(address) && port > 0);

		RpcNode rpcNode = new RpcNode();
		rpcNode.setIpAddress(address);
		rpcNode.setPort(port);
		return this.registerNode(rpcNode);
	}

	/**
	 * @see xj.toolkit.rpc.naming.RpcServerNamingService#unRegisterNode()
	 */
	@Override
	public void unRegisterNode() {
		Preconditions.checkArgument(StringUtils.isNoneBlank(nodeName));

		String nodeSrvKey = String.format(RpcRedisKeyConstants.RPC_NODE_SERVICE_LIST, nodeName);
		Set<String> svrs = jedis.smembers(nodeSrvKey);
		for (String string : svrs) {
			String svrKey = String.format(RpcRedisKeyConstants.RPC_SERVICE_NODE_LST, string);
			jedis.zrem(svrKey, nodeName);
		}

		String nodeKey = String.format(RpcRedisKeyConstants.RPC_NODE, nodeName);
		jedis.del(nodeKey);
	}

	/**
	 * @see xj.toolkit.rpc.naming.RpcServerNamingService#registerService(xj.toolkit.rpc.RpcService)
	 */
	@Override
	public void registerService(RpcService rpcService) {
		Preconditions.checkArgument(rpcService != null && StringUtils.isNoneBlank(rpcService.name()));

		String nodeSrvKey = String.format(RpcRedisKeyConstants.RPC_NODE_SERVICE_LIST, nodeName);
		jedis.sadd(nodeSrvKey, rpcService.name());

		String svrNodeKey = String.format(RpcRedisKeyConstants.RPC_SERVICE_NODE_LST, rpcService.name());
		jedis.sadd(svrNodeKey, nodeName);
	}

}
